<template>
    <el-card class="user-card">
        <div class="user-card-header">
            <div class="avatar-container">
                <img :src="user.avatar || 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif'"
                    class="user-avatar">
                <div class="upload-avatar">
                    <el-upload class="avatar-uploader" action="/api/upload" :show-file-list="false"
                        :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                        <span class="upload-btn">
                            <i class="el-icon-camera"></i>
                            <span>更换头像</span>
                        </span>
                    </el-upload>
                </div>
            </div>
            <div class="user-name">{{ user.name }}</div>
            <div class="user-role">{{ user.role }}</div>
        </div>
        <div class="user-info">
            <div class="info-item">
                <i class="el-icon-user"></i>
                <span class="info-label">账号:</span>
                <span>{{ user.userId }}</span>
            </div>
            <div class="info-item">
                <i class="el-icon-message"></i>
                <span class="info-label">邮箱:</span>
                <span>{{ user.email }}</span>
            </div>
            <div class="info-item">
                <i class="el-icon-date"></i>
                <span class="info-label">登录时间:</span>
                <span>{{ formatDate(user.loginTime) }}</span>
            </div>
        </div>
    </el-card>
</template>

<script>
export default {
    props: {
        user: {
            type: Object,
            required: true
        }
    },
    methods: {
        formatDate(date) {
            if (!date) return '';
            const d = new Date(date);
            return d.toLocaleDateString() + ' ' + d.toLocaleTimeString();
        },
        handleAvatarSuccess(res, file) {
            this.$message.success('头像上传成功');
            // 实际项目中这里应该调用修改头像的API
            this.$store.dispatch('user/setAvatar', URL.createObjectURL(file.raw));
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 或 PNG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        }
    }
}
</script>

<style lang="scss" scoped>
.user-card {
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);

    &-header {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 20px 0;
    }

    .avatar-container {
        position: relative;
        margin-bottom: 15px;

        .user-avatar {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            border: 2px solid #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .upload-avatar {
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 100%;
            border-radius: 50%;
            display: flex;
            justify-content: center;
            align-items: center;
            opacity: 0;
            transition: all 0.3s;
            background: rgba(0, 0, 0, 0.5);

            &:hover {
                opacity: 1;
            }

            .upload-btn {
                color: #fff;
                font-size: 14px;
                cursor: pointer;
                display: flex;
                flex-direction: column;
                align-items: center;

                i {
                    font-size: 24px;
                    margin-bottom: 5px;
                }
            }
        }
    }

    .user-name {
        font-size: 18px;
        font-weight: bold;
        margin-bottom: 5px;
    }

    .user-role {
        font-size: 14px;
        color: #909399;
        margin-bottom: 10px;
    }

    .user-info {
        padding: 20px;
        border-top: 1px solid #ebeef5;

        .info-item {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
            font-size: 14px;

            i {
                color: #409EFF;
                margin-right: 8px;
            }

            .info-label {
                color: #606266;
                margin-right: 8px;
                width: 50px;
            }
        }
    }
}
</style>